#include<iostream>
#include<memory>
#include"blockingQueue.hpp"
#include"pthread.hpp"
#include"task.hpp"
#include<time.h>
#include<unistd.h>


void consumerRoutine(void* argv)
{
    BlockingQueue<Task>* blockingQueue = static_cast<BlockingQueue<Task>*>(argv);
    while(true)
    {
        Task task;
        blockingQueue->pop(&task);
        task();
        task.print();
        sleep(1);
    }

}

void producerRoutine(void* argv)
{
    BlockingQueue<Task>* blockingQueue = static_cast<BlockingQueue<Task>*>(argv);
    const char ch[] = "+-*/";
    while(true)
    {
        int x = rand()%10;
        int y = rand()%10;
        char op = ch[rand()%4];
        Task task(x,y,op);
        blockingQueue->push(task);
        sleep(1);
    }
}

int main()
{
    srand((uint64_t)time(nullptr)^getpid());
    BlockingQueue<Task> blockingQueue;
    std::unique_ptr<thread> consumer(new thread(1, consumerRoutine, &blockingQueue));
    std::unique_ptr<thread> producer(new thread(1, producerRoutine, &blockingQueue));
    consumer->run();
    producer->run();
    consumer->join();
    producer->join();
    return 0;
}